using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MySql.Data.MySqlClient;
using System.Data;
using System.Configuration;

namespace TestProject
{
	
	[HandleError]
	public class UserController : Controller
	{
		public static List<User> users = new List<User>(0);
		
		public ActionResult Login (String u_name, String p_word)
		{
			User read = new User(u_name, p_word);
			if(u_name.Equals(read.username))
			{
				
				Session.Add("username", read.username);
				Session.Add("level", read.level);
				Session.Add("password", read.password);
				Session.Add("fname", read.firstname);
				Session.Add("lname", read.lastname);
				Session.Add("email", read.email);
				Session.Add("contactnumber", read.contactnumber);
				Session.Add("user_id", read.user_ID);
				Session.Add("isFirstTime", read.isFirstTime);
				
				if (read.isFirstTime)
					return RedirectToAction("FirstLogin");
				else{
					if(read.level == 1)
						return RedirectToAction ("Index");	
					else
						return RedirectToAction ("../Librarian");
				}
			}
			else
			{
				return RedirectToAction ("../Home/error");
				
			}
		}
		
		public ActionResult Profile(string id)
		{
			User user = TestProject.User.searchUsersByUsername(id, users);
			ViewData["reservations"] = user.getReservations();
			ViewData["loans"] = user.getLoans();
			ViewData["returned"] = user.getReturned();
			ViewData["user_ID"] = user.user_ID;
			ViewData["username"] = user.username;
			ViewData["firstname"] = user.firstname;
			ViewData["lastname"] = user.lastname;
			ViewData["contactnumber"] = user.contactnumber;
			ViewData["email"] = user.email;
			ViewData["level"] = user.level;
			return View();
		}
				
		public ActionResult Index ()
		{
			ViewData["Books"] = BookController.filteredBookList = BookController.searchedBooks;
			return View (ViewData);			
		}				
				
		public ActionResult FirstLogin ()
		{
			return View ();			
		}	
		
		public ActionResult Logout ()
		{
			Session.Abandon();			
			return RedirectToAction ("../Home");
		}
		
		[HttpPost]
		public object addUser(string username, string password, string firstname, string lastname, string email, string contact, int level)
		{
			User user = TestProject.User.addUser(username, password, firstname,lastname,email,contact,level);
			if (user!=null)
				UserController.users.Add(user);
            System.Web.Script.Serialization.JavaScriptSerializer serializeThis = new System.Web.Script.Serialization.JavaScriptSerializer();
            return  serializeThis.Serialize(user);
		}
		
		[HttpPost]
		public void deleteUser(int user_id)
		{
			TestProject.User.deleteUser(user_id);
			UserController.users = TestProject.User.getUsers();
		}
		
		[HttpPost]
		public bool editUser(int user_ID, string username, string password, string firstname, string lastname, string email, string contact, int level)
		{
			bool toReturn = TestProject.User.editUser(user_ID,username,password,firstname,lastname, email, contact, level);
			if (toReturn)
				UserController.users = TestProject.User.getUsers();
			return toReturn;
		}
		
		[HttpPost]
		public bool editAccount(string username, string firstname, string lastname, string email, string contact)
		{
			bool toReturn;
			toReturn = TestProject.User.editUser((int)Session["user_id"],username,firstname,lastname, email, contact);
			if (toReturn)
			{
				User read = new User((int)Session["user_id"]);
				Session["username"] = read.username;
				Session["fname"] = read.firstname;
				Session["lname"] = read.lastname;
				Session["email"] = read.email;
				Session["contactnumber"] = read.contactnumber;
				Session["isFirstTime"] = read.isFirstTime;
				return true;
			}
			else
				return toReturn;
		}
		
		[HttpPost]
		public object getUser()
		{
			User user = new User((int)Session["user_id"]);        
            System.Web.Script.Serialization.JavaScriptSerializer serializeThis = new System.Web.Script.Serialization.JavaScriptSerializer();
            return serializeThis.Serialize(user);
		}
		
		[HttpPost]
		public object adminGetUser(int user_ID)
		{
			User user = new User(user_ID);        
            System.Web.Script.Serialization.JavaScriptSerializer serializeThis = new System.Web.Script.Serialization.JavaScriptSerializer();
            return serializeThis.Serialize(user);
		}
		
		[HttpPost]
		public bool register(string username, string firstname, string lastname, string email, string contactnumber)
		{
			return TestProject.Signup.addSignUp(username, firstname, lastname, email, contactnumber);
		}
		
		// TODO: add validation of contact number
		public bool validateInformation(string email, string contactnumber)
		{
			return TestProject.User.validateEmail(email);
		}
		
		public object SearchUser(string search)
		{
			List<User> searchedUser = TestProject.User.searchUsers(search, users);
			System.Web.Script.Serialization.JavaScriptSerializer serializeThis = new System.Web.Script.Serialization.JavaScriptSerializer();
			return  serializeThis.Serialize(searchedUser);
		}

		public ActionResult Manage()
		{
			ViewData["Users"] = TestProject.User.getUsers();
			return View();
		}
		
		public ActionResult Account()
		{
			return View();			
		}
		
		public bool changePassword(string oldPassword, string newPassword)
		{
			return TestProject.User.changePassword((int)Session["user_id"], oldPassword, newPassword);
		}
		
		public void adminChangePassword(int user_ID, string password)
		{
			TestProject.User.changePassword(user_ID, password);
		}
		
		[HttpPost]
		public int changePasswordFirstLogin(string password)
		{
			TestProject.User.changePasswordFirstLogin((int)Session["user_id"], (string)Session["password"], password);
			User current = new User((int)Session["user_id"]);
			current.isFirstTime = false;
			Session["password"] = password;
			Session["isFirstTime"] = false;
			return (int)Session["level"];
		}
		
		public ActionResult Loans()
		{
			ViewData["loans"] = (new User((int)Session["user_id"])).getLoans();
			return View();
		}
		
		public ActionResult Returned()
		{
			ViewData["returned"] = (new User((int)Session["user_id"])).getReturned();
			return View();
		}
		
		public ActionResult Reservations()
		{
			ViewData["reservations"] = (new User((int)Session["user_id"])).getReservations();
			return View();
		}
		
		public void cancelReservation(int reservation_ID)
		{
			Reservation.deleteReservation(reservation_ID);
		}
	}			
}


